/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.model;

import java.util.List;
import java.util.Map;

import android.os.Parcel;
import android.os.Parcelable;
import dk.redia.lib.log.Log;

public class Library implements Parcelable, Comparable<Library> {
	private static final String logTag = "Library";
	private String id;
	private String name;
	private String city;
	private String fax;
	private Boolean open;
	private String phone;
	private String street;
	private Integer priority;
	
	public static Library findByAbbr(List<Library> haystack, String needle) {
		for (Library library : haystack) {
			if (library.id.equals(needle)) {
				return library;
			}
 		}
		return null;
	}
	
	public static Library fromMap(Map<String, Object> map) {
		try {
			Library l = new Library();
			if (!map.containsKey("id")) {
				Log.e(logTag, "id field missing in library map");
				return null;
			}
			if (!map.containsKey("fax")) {
				Log.e(logTag, "fax field missing in library map");
				return null;
			}
			if (!map.containsKey("city")) {
				Log.e(logTag, "city field missing in library map");
				return null;
			}
			if (!map.containsKey("street")) {
				Log.e(logTag, "street field missing in library map");
				return null;
			}
			if (!map.containsKey("name")) {
				Log.e(logTag, "name field missing in library map");
				return null;
			}
			if (!map.containsKey("phone")) {
				Log.e(logTag, "phone field missing in library map");
				return null;
			}
			if (!map.containsKey("priority")) {
				Log.e(logTag, "priority field missing in library map");
			}
			
			l.id = (String)map.get("id");
			l.name = (String)map.get("name");
			l.city = (String)map.get("city");
			l.fax = (String)map.get("fax");
			l.phone = (String)map.get("phone");
			l.open = (Boolean)map.get("open");
			l.street = (String)map.get("street");
			l.priority = (Integer)map.get("priority");
			
			return l;
		} catch (ClassCastException e) {
			e.printStackTrace();
			Log.e(logTag, "Failed to convert field in library map: " + e.getMessage());
			return null;
		}
	}
	
	public Library() {
		
	}

	public String getId() {
		return id;
	}

	public int getPriority() {
		return this.priority;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isOpen() {
		return open;
	}

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(id);
		dest.writeString(city);
		dest.writeString(phone);
		dest.writeString(fax);
		dest.writeString(name);
		dest.writeString(street);
		dest.writeInt(priority);
	}
	
	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public static final Parcelable.Creator<Library> CREATOR = new Parcelable.Creator<Library>() {

		public Library createFromParcel(Parcel source) {
			Library l = new Library();
			l.id = source.readString();
			l.city = source.readString();
			l.phone = source.readString();
			l.fax = source.readString();
			l.name = source.readString();
			l.street = source.readString();
			l.priority = source.readInt();
			
			return l;
		}

		public Library[] newArray(int size) {
			return new Library[size];
		}
		
	};

	public int compareTo(Library another) {
		if (this.priority==null) {
			return -1;
		}
		if (another.priority==null) {
			return 1;
		}
		// Priority is actually a weight, so
		// high priority value comes before a low one
		return this.priority.compareTo(another.priority) * -1;
	}
}
